package bintree.leetcode;

//958. 二叉树的完全性检验

import java.util.Deque;
import java.util.LinkedList;

public class Num958_isCompleteTree {

    public boolean isCompleteTree(TreeNode root) {
        Deque<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        boolean isSecondStep=false;
        while (!queue.isEmpty()){
            TreeNode cur=queue.poll();
            if(!isSecondStep){
                if(cur.left!=null && cur.right!=null){
                    queue.offer(cur.left);
                    queue.offer(cur.right);
                }else if(cur.left!=null){
                    isSecondStep =true;
                    queue.offer(cur.left);
                }else if(cur.right!=null){
                    return false;
                }else {
                    isSecondStep=true;
                }
            }else {
                if(cur.left!=null || cur.right!=null){
                    return false;
                }
            }
        }
        return true;
    }
}
